package com.ruoyi.web.controller.fenxi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ROrg;
import com.ruoyi.system.domain.ROrgRiskEvent;
import com.ruoyi.system.service.IROrgRiskEventService;
import com.ruoyi.system.service.IROrgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 风险事件列Controller
 * 
 * @author ruoyi
 * @date 2022-09-25
 */
@Controller
@RequestMapping("/system/event")
public class ROrgRiskEventController extends BaseController
{
    private String prefix = "system/event";
    @Autowired
    private IROrgService rOrgService;
    @Autowired
    private IROrgRiskEventService rOrgRiskEventService;

//    @RequiresPermissions("system:event:view")
    @GetMapping()
    public String event(Long orgId, ModelMap mmap)
    {
        mmap.put("orgId",orgId);
        List<ROrg> rOrgs = rOrgService.selectROrgList(new ROrg());
        mmap.put("rOrgs", rOrgs);
        return prefix + "/event";
    }

    /**
     * 查询风险事件列列表
     */
//    @RequiresPermissions("system:event:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ROrgRiskEvent rOrgRiskEvent)
    {
        startPage();
        List<ROrgRiskEvent> list = rOrgRiskEventService.selectROrgRiskEventList(rOrgRiskEvent);
        return getDataTable(list);
    }

    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo glist(ROrgRiskEvent rOrgRiskEvent)
    {
        startPage(1,20);
//        List<ROrgRiskEvent> list = (List<ROrgRiskEvent>) CacheUtils.get(CacheUtils.EVENT_KEY+rOrgRiskEvent.getOrgId());
//        if(Objects.isNull(list)||list.isEmpty()){
//            list = rOrgRiskEventService.selectROrgRiskEventList(rOrgRiskEvent);
//            CacheUtils.put(CacheUtils.EVENT_KEY+rOrgRiskEvent.getOrgId(),list);
//        }
        List<ROrgRiskEvent> list =   rOrgRiskEventService.selectROrgRiskEventList(rOrgRiskEvent);
        return getDataTable(list);
    }

    /**
     * 导出风险事件列列表
     */
//    @RequiresPermissions("system:event:export")
    @Log(title = "风险事件列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ROrgRiskEvent rOrgRiskEvent)
    {
        List<ROrgRiskEvent> list = rOrgRiskEventService.selectROrgRiskEventList(rOrgRiskEvent);
        ExcelUtil<ROrgRiskEvent> util = new ExcelUtil<ROrgRiskEvent>(ROrgRiskEvent.class);
        return util.exportExcel(list, "风险事件列数据");
    }

    /**
     * 新增风险事件列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存风险事件列
     */
//    @RequiresPermissions("system:event:add")
    @Log(title = "风险事件列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ROrgRiskEvent rOrgRiskEvent)
    {
        return toAjax(rOrgRiskEventService.insertROrgRiskEvent(rOrgRiskEvent));
    }

    /**
     * 修改风险事件列
     */
//    @RequiresPermissions("system:event:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ROrgRiskEvent rOrgRiskEvent = rOrgRiskEventService.selectROrgRiskEventById(id);
        mmap.put("rOrgRiskEvent", rOrgRiskEvent);
        return prefix + "/edit";
    }

    /**
     * 修改保存风险事件列
     */
//    @RequiresPermissions("system:event:edit")
    @Log(title = "风险事件列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ROrgRiskEvent rOrgRiskEvent)
    {
        return toAjax(rOrgRiskEventService.updateROrgRiskEvent(rOrgRiskEvent));
    }

    /**
     * 删除风险事件列
     */
//    @RequiresPermissions("system:event:remove")
    @Log(title = "风险事件列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rOrgRiskEventService.deleteROrgRiskEventByIds(ids));
    }
}
